﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Ribbon;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Windows;
using System.IO;
using System.Reflection;

namespace RibbonCreator
{
    public partial class ToolRibbon
    {
        private RibbonTab ribbonTab;

        [CommandMethod("CreateRibbonTab")]
        public void CreateRibbonTab()
        {
            try
            {
                // 创建Ribbon Tab页
                ribbonTab = new RibbonTab();
                var appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                this.LoadPanelsFromXml(appPath + @"\AppUI\ribbonTab.xml");

                // tab管理器
                RibbonControl ribCntrl = RibbonServices.RibbonPaletteSet.RibbonControl;

                // 删除之前的tab
                var index = -1;
                for (int i = 0; i < ribCntrl.Tabs.Count; i++)
                {
                    if (ribCntrl.Tabs[i].Id == ribbonTab.Id)
                    {
                        index = i;
                        break;
                    }
                }
                if (index != -1)
                {
                    ribCntrl.Tabs.RemoveAt(index);
                }

                ribCntrl.Tabs.Add(ribbonTab);
            }
            catch (System.Exception e)
            {
                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(e.Message);
            }
        }
    }
}
